/**
 * 
 */
package pt.inescid.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

import java.io.*;


/**
 * @author Sergio Esteves (sesteves at gsd.inesc-id.pt)
 * 
 */
public class Log {

	private static final String dateTimeFormat = "dd/MM/yyyy HH:mm:ss:SSS";
	
	// log levels
    public static final int SEVERE = 60;
    public static final int WARNING = 50;
    public static final int INFO = 40;
    public static final int CONFIG = 30;
    public static final int FINE = 20;
    public static final int FINER = 10;
    public static final int FINEST = 0;
	
	private int level;
	private String fileName;	
	private SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateTimeFormat);
	
	private String getDateTime(){
		 return simpleDateFormat.format(new Date());
	}
	
	public Log(String filename, int level){
		this.fileName = filename;
		this.level = level;
	}
	
	private synchronized void writeInFile(String line) {
		try {
			FileOutputStream fout = new FileOutputStream (fileName, true);
			new PrintStream(fout).println(line);
			fout.close();
		} catch (IOException e) {
			System.err.println("Error: " + e.getMessage());
		}
	}
	
	public void write(Object o, String message, int level){
		if(level >= this.level) {
			String line = "[(" + getDateTime() + ") " + o.getClass().getSimpleName() + "] " + message;
			writeInFile(line);
		}
	}
	
	public void write(String message, int level){
		if(level >= this.level) {
			String line = "[" + getDateTime() + "] " + message;
			writeInFile(line);
		}
	}
}
